﻿@{
    ViewData["Title"] = "JavaScript";
}

<h1>JavaScript</h1>


<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<button class="btn btn-secondary bg-success" id="btnGetData">获取数据</button>
<script>

    function callbackFunction(result) {
        console.log(result);
    }

    $(document).ready(() => {
        $("#btnGetData").click(() => {
            var script = document.getElementById("scriptJsonp");
            if (script) {
                script.parentNode.removeChild(script);
            }
            var jsonp = document.createElement("script");
            jsonp.type = "text/javascript";
            jsonp.src = "https://localhost:7111/api/jsonp/GetDataByJavaScript?jsoncallback=callbackFunction";
            jsonp.id = "scriptJsonp";
            document.getElementsByTagName("head")[0].appendChild(jsonp);
        });
    });
</script>


<button class="btn btn-secondary bg-success" id="btnGetData1">获取数据</button>
<script>
    $(document).ready(() => {
        $("#btnGetData1").click(() => {
            var script = document.getElementById("scriptJsonpData");
            if (script) {
                script.parentNode.removeChild(script);
            }
            var jsonp = document.createElement("script");
            jsonp.type = "text/javascript";
            jsonp.src = "https://localhost:7111/js/content.txt";
            jsonp.id = "scriptJsonpData";
            document.querySelector('head').appendChild(jsonp);
        });
    });
    function jsonpData(data){
        console.log(data);
    }
</script>